package com.aha.dataanalyzeandview.controller;


import com.aha.dataanalyzeandview.entity.base.ResultEntity;
import com.aha.dataanalyzeandview.entity.wxrobot.WxRobotSettingsEntity;
import com.aha.dataanalyzeandview.service.IWxRobotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/wxRobot")
public class WxRobotController {

    @Autowired
    private IWxRobotService wxRobotService;


    /**
     * 查询所有的机器人设置信息
     * @param r
     * @return
     */
    @GetMapping("/queryWxRobotSettingsInfo")
    public ResultEntity queryWxRobotSettingsInfo(HttpServletRequest r){
        String keyword = r.getParameter("keyword");

        Integer id = 0;
        String idStr = r.getParameter("id");
        if(idStr != null){
            id = Integer.valueOf(idStr);
        }
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("keyword", keyword);
        paramMap.put("id", id);
        List<WxRobotSettingsEntity> wxRobotSettingsEntities = wxRobotService.queryWxRobotSettingsInfo(paramMap);
        return  ResultEntity.success(wxRobotSettingsEntities);
    }

    /**
     * 添加所有的机器人设置信息
     * @param settings
     * @return
     */
    @PostMapping(value = "/addWxRobotSettingsInfo")
    public ResultEntity addWxRobotSettingsInfo(WxRobotSettingsEntity settings){
        int res = wxRobotService.addWxRobotSettingsInfo(settings);
        if(res > 0 ){
            return  ResultEntity.success();
        }else{
            return ResultEntity.error(-22,"添加发信机器人失败！");
        }
    }


    /**
     * 修改发信机器人的状态
     * @param r
     * @return
     */
    @PostMapping(value = "/updateSendMsgRobotStatus")
    public ResultEntity updateSendMsgRobotStatus(HttpServletRequest r){
        String id = r.getParameter("id");
        Integer status = Integer.parseInt(r.getParameter("status"));
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id",Integer.valueOf(id));
        paramMap.put("status",status);
        int res = wxRobotService.updateSendMsgRobotStatus(paramMap);

        return  ResultEntity.success();
    }


    /**
     * 删除设置的发信机器人
     * @param id
     * @return
     */
    @PostMapping(value = "/delRobotSettings")
    public ResultEntity delRobotSettings(@RequestParam("id") Integer id ){

        int res = wxRobotService.delRobotSettings(id);
        if(res > 0 ){
            return  ResultEntity.success();
        }else{
            return ResultEntity.error(-22,"删除发信机器人失败！");
        }
    }


}
